/*******************************************************************************
 * Package: com.chris.gateway.filters
 * Type:    LogGatewayFilterFactory
 * Date:    2024/3/7 下午5:06
 *
 * Copyright (c) 2024 BOING CORP.,LTD All Rights Reserved.
 *
 * You may not use this file except in compliance with the License.
 *******************************************************************************/
package com.chris.gateway.filters;

import lombok.Data;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.Arrays;
import java.util.List;

/**
 * 自定义局部过滤器
 *
 * @author Hudesheng
 * @date 2024/3/7 下午5:06
 */
@Component
public class LogGatewayFilterFactory extends AbstractGatewayFilterFactory<LogGatewayFilterFactory.Config> {
    //构造函数
    public LogGatewayFilterFactory() {
        super(LogGatewayFilterFactory.Config.class);
    }
    
    /**
     *
     * @param config
     * @return
     */
    @Override
    public GatewayFilter apply(Config config) {
        return new GatewayFilter() {
            @Override
            public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
                if(config.consoleLog){
                    System.out.println("控制台日志已开启！");
                }
                if(config.cacheLog){
                    System.out.println("缓存日志已开启！");
                }
                return chain.filter(exchange);
            }
        };
    }
    
    /**
     * 读取配置文件中的参数赋值到配置类中
     * @return
     */
    @Override
    public List<String> shortcutFieldOrder() {
        return Arrays.asList("consoleLog","cacheLog");
    }
    
    @Data
    public static class Config{
        private boolean consoleLog;
        private boolean cacheLog;
    }
}